package Adventure;

import java.util.ArrayList;

/**
 * The player class, with methods to handle player's
 * actions and status.
 * @author Robin
 */
public class Player
{
	private int playerExperience;
	private int playerLevel;
	private int playerHealth;
	private int playerArmor;
	
	private int totalAmmo = 0;
	
	private String playerName;
	private GameObject equipped;
	private ArrayList<GameObject> backpack;
	
	private boolean playerStatus;
	private boolean equippedItem;
	private boolean hasAmmo = false;
		
	private Tile currentLocation;
	private int xPos;
	private int yPos;
		
	public Player(String _name, int _health, int _armor,
			int _level)
	{
		playerName = _name;
		playerHealth = _health;
		playerArmor = _armor;
		playerLevel = _level;
		
		playerStatus = true;
		equippedItem = false;
		
		backpack = new ArrayList<GameObject>();
		currentLocation = null;
	}

	public void setCurrentLocation(Tile _pos, int _x, int _y)
	{
		currentLocation = _pos;
		xPos = _x;
		yPos = _y;
	}
	
	public boolean hasAmmo()
	{
		if(totalAmmo == 0)
		{
			for(int i = 0; i< backpack.size(); i++)
			{
				if(backpack.get(i) instanceof Projectile)
					backpack.remove(i);
			}
			hasAmmo = false;
		}
		else
			hasAmmo = true;
		
		return hasAmmo;
	}
	
	public void addBullet(int _amount)
	{
		totalAmmo += _amount;
		System.out.println("Added " + _amount + " bullets to your backpack ( Inventory )");
	}
	
	public int dropBullet(int _amount)
	{
		if(_amount > totalAmmo)
		{
			System.out.println("The amount of bullet's you've specified is more than you've got in your bags.");
			return 0;
		}
		else
		{
			return _amount;
		}
	}
	
	public int getTotalAmmo()
	{
		return totalAmmo;
	}
	
	public void shoot()
	{
		totalAmmo--;
	}
	
	public int getGridX()
	{
		return xPos;
	}
	
	public int getGridY()
	{
		return yPos;
	}
	
	public Tile getCurrentLocation()
	{
		return currentLocation;
	}
	
	public int getPlayerLevel()
	{
		if(playerExperience >= 0 && playerExperience < 50)
		{
			playerLevel = 1;
			
		}
		else if(playerExperience >= 50 && playerExperience < 100)
		{
			playerLevel = 2;
			
		}
		else if(playerExperience >= 100 && playerExperience < 200)
		{
			playerLevel = 3;
			
		}
		else if(playerExperience >= 200 && playerExperience < 300)
		{
			playerLevel = 4;
	
		}
		else if(playerExperience >= 300 && playerExperience < 400)
		{
			playerLevel = 5;
		
		}
		else if(playerExperience >= 400 && playerExperience < 500)
		{
			playerLevel = 6;
			
		}
		else if(playerExperience >= 500 && playerExperience < 750)
		{
			playerLevel = 7;
			
		}
		else if(playerExperience >= 750 && playerExperience < 1000)
		{
			playerLevel = 8;
			
		}
		else if(playerExperience >= 1000 && playerExperience < 1500)
		{
			playerLevel = 9;
			
		}
		else
		{
			playerLevel = 10;
			
		}
		
		return playerLevel;
	}

	public void setPlayerLevel(int _playerLevel) 
	{
		playerLevel = _playerLevel;
	}
	
	public void addExperience(int _exp)
	{
		System.out.println("You gained " + _exp + " experience");
		playerExperience += _exp;
	}

	public int getPlayerHealth() 
	{
		return playerHealth;
	}

	public void setPlayerHealth(int _playerHealth)
	{
		playerHealth = _playerHealth;
	}

	public int getPlayerArmor()
	{
		return playerArmor;
	}

	public void setPlayerArmor(int _playerArmor)
	{
		playerArmor = _playerArmor;
	}

	public String getPlayerName()
	{
		return playerName;
	}

	public void setPlayerName(String _playerName)
	{
		playerName = _playerName;
	}

	public GameObject getEquipped() 
	{
		return equipped;
	}

	public void setEquipped(GameObject _equipped)
	{
		System.out.println("Equipping " + _equipped + " to your equipped slot");
		equipped = _equipped;
		equippedItem = true;
	}

	public boolean isPlayerStatus() 
	{
		return playerStatus;
	}

	public void setPlayerStatus(boolean _playerStatus)
	{
		playerStatus = _playerStatus;
	}

	public boolean isEquippedItem() 
	{
		return equippedItem;
	}

	public void setEquippedItem(boolean _equippedItem) 
	{
		equippedItem = _equippedItem;
	}

	public ArrayList<GameObject> getBackpack() 
	{
		return backpack;
	}
	
	public String toString()
	{
		return playerName + " is a \"race\" and are level " + playerLevel;
	}
	
	public void printStatus()
	{
		System.out.println("Name: \t\t" + playerName);
		System.out.println("Level: \t\t" + playerLevel);
		System.out.println("Health: \t" + playerHealth);
		System.out.println("Armor: \t\t" + playerArmor);
	}
	
	public void pickupObject(GameObject _object)
	{
		boolean hasBullets = false;
		
		for(int i = 0; i < backpack.size(); i++)
		{
			if(backpack.get(i) instanceof Projectile)
			{
				hasBullets = true;
			}
		}
		
		if(_object instanceof Projectile && hasBullets == true)
		{
			System.out.println("Added bullets to your inventory");
			hasBullets = false;
		}
		else
		{
			System.out.println("Picked up and placed " + _object + " in your backpack ( Inventory )");
			backpack.add(_object);
		}
	}
	
	public GameObject dropItem(GameObject _object)
	{
		for(int i = 0; i < backpack.size(); i++)
		{
			if(backpack.get(i).equals(_object))
			{
				return backpack.remove(i);
			}
		}
		
		System.out.println("The item is not found "+_object);
		return null;
	}
	
	public void displayInventory()
	{
		int index = 1;
		
		for(int itemIndex = 0; itemIndex < backpack.size(); itemIndex++)
		{
			if(backpack.get(itemIndex) instanceof Projectile)
			{
				System.out.println(index + ") " + backpack.get(itemIndex) + ": " + totalAmmo);
			}
			else
			{
				System.out.println(index + ") " + backpack.get(itemIndex));
			}
			
			index++;
		}
	}
	
}
